import 'package:media_kit/media_kit.dart';
import 'package:situation/data/bus/intercom_bus.dart';
import 'package:situation/data/channel/intercom_channel.dart';
import 'package:situation/data/database/sql_provider.dart';
import 'package:situation/data/store/auth_store.dart';
import 'package:situation/data/store/config_store.dart';
import 'package:situation/data/store/server_url_store.dart';
import 'package:situation/domain/db/system_config_domain.dart';
import 'package:situation/plugins/mkim/mkim.dart';
import 'package:situation/shared/shared.dart';
import 'package:situation/shared/util/util_location.dart';
import 'package:situation/shared/util/util_mqtt.dart';
import 'package:situation/shared/util/util_sp.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class AppInitializer {
  // APP 初始化
  static init() async {
    // 初始化 Flutter 框架
    WidgetsFlutterBinding.ensureInitialized();

    /// 隐藏状态栏。为启动页、引导页设置。完成后修改回显示状态栏。
    SystemChrome.setEnabledSystemUIMode(
      SystemUiMode.manual,
      overlays: [SystemUiOverlay.bottom],
    );
    // TODO(weilu): 启动体验不佳。状态栏、导航栏在冷启动开始的一瞬间为黑色，且无法通过隐藏、修改颜色等方式进行处理。。。
    // 相关问题跟踪：https://github.com/flutter/flutter/issues/73351

    // 视频播放组件
    MediaKit.ensureInitialized();

    // 初始化 SharedPreferences Util.
    await UtilSp.getInstance();

    // 初始化数据库
    SqlProvider.instance;

    // 安卓 flutter 沉浸式透明状态栏 #安卓状态栏设置成透明
    if (GetPlatform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.light,
      );
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }

    await initStore();

    // 连接mqtt
    connectMqtt();

    connectIm();
    // 设置 easyRefresh
    EasyRefresh.defaultFooterBuilder = () => TaurusFooter();
    EasyRefresh.defaultHeaderBuilder = () => TaurusHeader();
  }

  // 初始化基本缓存
  static  initStore() async {
    // 前置判断
    if (!UtilSp.haveKey(AppConstant.systemConfigIndex)!) {
      UtilSp.putInt(AppConstant.systemConfigIndex, 0);
      UtilSp.putString(
        AppConstant.systemConfigIntercomUrl,
        SystemConfigDomain.initData[0]["intercomUrl"],
      );
      UtilSp.putString(
        AppConstant.systemConfigServerUrl,
        SystemConfigDomain.initData[0]["serverUrl"],
      );
    }

    Get.put<AuthStore>(AuthStore());
    Get.put<ConfigStore>(ConfigStore());
    Get.put<IntercomChannel>(IntercomChannel());
    Get.put<IntercomBus>(IntercomBus());
    Get.put<MkIm>(MkIm());
  }

  static void connectMqtt() async {
    await AuthStore.to.getProfile();

    // 检测是否有token
    if (AuthStore.to.isLogin) {
      //  建立连接
      UtilMqtt().connect();
      Future.delayed(Duration(seconds: 3), () {
        IntercomChannel.to.login(
          AuthStore.to.profile.gwsdUsername,
          AuthStore.to.profile.gwsdPassword,
        );
      });
    }
  }


  static void connectIm() async {
    await AuthStore.to.getProfile();

    // 检测是否有token
    if (AuthStore.to.isLogin) {
      //  建立连接
      print("sfz: " + AuthStore.to.profile.idcard!);
      MkIm.to.login(AuthStore.to.profile.idcard, AuthStore.to.profile.idcard);
    }
  }
}
